%  
%  \iffalse  
% 
%    The first part is a comment to the reader(s) of `version.dtx'.
%
%  version.dtx    Version 1.0, Dec. 28, 1994
%  (c) 1994 by Mats Dahlgren  (matsd@physchem.kth.se)
%
%  Please see the information in file `version.ins' on how you 
%  may use and (re-)distribute this file.  Run LaTeX on the file 
%  `version.ins' to get a .sty-file and instructions.
%
%  This file may NOT be distributed if not accompanied by 'version.ins'.
%
%  \fi
%
%  \def\filename{version.dtx}
%  \def\fileversion{1.0}
%  \def\filedate{1994/12/28}\def\docdate{\filedate}
%  \changes{1.0}{1994/12/28}{First version of `version' package.}
%  \MakeShortVerb{\|}
%  \title{\LaTeX{} Macro for Version Numbering of Files}
%  \author{Mats Dahlgren\\ (\texttt{matsd@physchem.kth.se})}
%  \maketitle
%  \begin{abstract}
%  The |version| package provides a user-friendly way to
%  introduce file version numbers in \LaTeX{} documents. 
%  It remembers the previous version number, also when
%  the |.aux|-file is corrupted (due to errors in the
%  \LaTeX{} run).\\ \small This file and the package:\
%  Copyright \copyright\ 1994 by Mats Dahlgren.  All rights
%  reserved. 
%  \end{abstract}
%  
%  \section{Introduction}
%  This package provides one command, |\version|, which
%  puts a version number where it appears.  The version
%  number is increased each time \LaTeX{} is run,
%  \textit{i.e.}\ it numbers the |.dvi|-file.  If the
%  package is loaded but the command |\version| is not
%  issued, the present version number is preserved. 
%  Numbering can be incremented at three different levels: 
%  units, tenths, and hundreds.  A change between two of these 
%  is obtained by changing the package option.  After a change
%  of steplength, the previous version number is
%  incremented with the new steplength.  The version number
%  can be held constant by using the command
%  |\keepversion|; this enables the version number to be
%  printed without being increased.  (|\keepversion| can be
%  overridden by the command |\stepversion|.) 
%  
%  \section{History}
%  This is the first version of the package.  The package
%  was created with useful help and ideas from Johan
%  Fr\"oberg (\texttt{emgion@physchem.kth.se}). \par
%  The |version| package has been tested with \LaTeXe%  
%  /\TeX{} 3.1415 under OpenVMS. Please send bug reports
%  (see below), corrections, additions, suggestions,
%  \textit{etc.}\ to me at \texttt{matsd@physchem.kth.se}. 
%  
%  \section{``Userguide''}
%  \subsection{Requirements}     
%  The file |version.sty| must be available in the user's
%  |TEXINPUTS| directories. It requires \LaTeXe{} of
%  1994/06/01 (or newer). 
%  
%  \subsection{Usage}
%  The package is included by stating \\
%  |  \usepackage[|\textit{option}|]{version}|\\
%  in the document preamble.  It can take any of the 
%  options |one|, |ten|, and |hundred|. To produce a 
%  version number anywhere in your document you issue the 
%  command |\version| at the desired place in your \LaTeX{}
%  input file.\footnote{Notice that the text ``Version''
%  is \textit{not} produced by the
%  $\backslash$\texttt{version} command.}  The 
%  version number will then be incremented  each time you
%  run \LaTeX{} on the file.  Depending on the  option
%  used, the version number will be an integer (no  option
%  or option |one|), a number with one decimal  (option
%  |ten|), or a two-decimal number (option  |hundred|). 
%  When changing from one option to another,  the previous
%  format of the version number will be  changed according
%  to the new option.  If a smaller  increment is
%  specified, the next increment will simply  append one
%  digit |1| to the old version number; \textit{e.g.}\  a
%  change from option |ten| to |hundred| after version  3.2
%  will result in version 3.21.  If a larger  increment is
%  specified, the old version number will be truncated
%  before the increment; \textit{e.g.}\  a change from     
%  option |hundred| to |ten| after version 3.25
%  will  result in version 3.3.  You should (normally) not
%  use the  command |\version| more once in a document. 
%  The version number is stored in a file with  extension
%  `|vrs|' in the default directory.  (The  full name of this
%  file is |\jobname.vrs|.) 
%  
%  If you want your document to contain a version 
%  number without having it incremented each time
%  you run \LaTeX{}, you should issue the command 
%  |\keepversion| in the document (preamble).  This 
%  switches off the incrementation mechanism and prevents
%  the package from writing an updated |.vrs|-file.  
%  The |\keepversion| command can be cancelled at 
%  any later place in the document by issuing the 
%  command |\stepversion|.  The action taken by the 
%  |\version| command is determined by which of these
%  two was last issued.
%  
%  Typically, you would load the |version| package 
%  with the desired option and use the command 
%  |\version| where you want your document to state 
%  its version number.  Normally, you would have the
%  |\keepversion| command in the preamble, but 
%  comment it out on the first \LaTeX{} run when you 
%  prepare to print a new version and then have the 
%  |\keepversion| in action during the \LaTeX{} runs 
%  needed to resolve the references.
%  
%  When it is desired to repeat the version number
%  several times in a document, issue a 
%  |\keepversion| directly after the first use of 
%  |\version|.  This will then produce the same 
%  version number at all occurrences; and the version
%  number will be incremented according to the use of 
%  |\keepversion| in the preamble.  
%  
%  If the increase of the version number is to occur 
%  at any stage later than the fist occurrence of 
%  |\version|, the occurrence of |\version| 
%  which is to increase the version number should be
%  preceeded by a |\stepcounter| command (and 
%  followed by |\keepversion| to prevent further
%  increments).  The author can not think of any 
%  reason why this should be desired, but it can be 
%  achieved anyway$\ldots$
%  
%  \section{Known Problems}
%  \begin{itemize}
%  \item At the present, there is something going on in the
%  macro |\@skrivner| which is causing \LaTeX{} to typeset 
%  some extra blank space before the version number produced
%  by |\version| when |\keepversion| is \textit{not} in effect.
%  To avoid this problem, run \LaTeX{} on your input file 
%  one extra time with no increment of the version number before
%  you print your document (you may use this extra run to resolve
%  your references as well).  This will hopefully be 
%  avoided in future release(s) of the package.
%  \item The |\version| package is not working properly with the
%  |\include{|\textit{file}|}| command; use |\input{|\textit{file}|}| 
%  instead.
%  \item If the command |\version| is issued more than once
%  in a document,   the version number will be incremented
%  at each occurrence and produce   different version
%  numbers at the different occurrences (unless  
%  |\keepversion| is in effect). 
%  \end{itemize} 
%  
%  \section{Sending a Bug Report}
%  |version| is most likely to contain bugs.
%  Reports of bugs in the package are most welcome.  
%  Before filing a bug report,
%  please take the following actions:
%  \begin{enumerate}
%  \item Ensure your problem is not due to your inputfile;
%  \item Ensure your problem is not due to 
%     your own package(s) or class(es);
%  \item Ensure your problem is not covered in the section 
%     ''Known Problems'' above;
%  \item  Try to locate the problem by writing a minimal 
%     \LaTeX{} input file which reproduces the problem.  
%     Include the command\\ 
%     |  \setcounter{errorcontextlines}{999}|\\ 
%     in your input;
%  \item Run your file through \LaTeX ;
%  \item Send a description of your problem, the input file 
%     and the log file via e-mail to:\\  \hspace*{5mm}
%     \texttt{matsd@physchem.kth.se}.
%  \end{enumerate}
%  \bigskip
%  
%  {\itshape Enjoy your \LaTeX!\raisebox{-\baselineskip}{mats d.}}
%  
%  \section{The Code} 
%  For the interested reader(s), here is a short description 
%  of the code.
%
%  First, the package should identify itself:
% \iffalse
%<*paketkod>
% \fi
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{version}%
  [1994/12/28 Version numbering of LaTeX files (v. 1.0).]
\typeout{The `version' package included.  (c) 1994 by Mats Dahlgren.}
%    \end{macrocode}
%  \par Next, the options are declared.  The declaration of
%  option |one| is a bit superfluous, but supplied for 
%  symmetry reasons.  All it does is telling the user 
%  she/he is using the default option.
%    \begin{macrocode}
\DeclareOption{one}{%
  \typeout{Option `one' is default for `version'.}
}
%    \end{macrocode}
%  The |ten| option modifies the |\version| command to 
%  produce a decimal number and steps the integer version 
%  number if the decimal part reaches 10.  If the boolean 
%  |KeepVersion| (set by the command |\keepversion|) is 
%  true, no increase of the version number is preformed, 
%  nor is anything written to the |.vrs|-file.  The command 
%  |\@skrivner| is responsible for writing the |.vrs|-file  
%  (``skriv ner'' is Swedish for ``write down'').
%    \begin{macrocode}
\DeclareOption{ten}{%
  \renewcommand{\version}{%
    \ifKeepVersion \else
      \stepcounter{versionnrten}
      \ifnum\theversionnrten=10\stepcounter{versionnr} \fi
      \@skrivner
    \fi
    \theversionnr .\theversionnrten
  }
}
%    \end{macrocode}
%  The option |hundred| does the same as |ten|, but with 
%  a two-decimal number.
%    \begin{macrocode}
\DeclareOption{hundred}{%
  \renewcommand{\version}{%
    \ifKeepVersion \else 
      \stepcounter{versionnrhundred}
      \ifnum\theversionnrhundred=10\stepcounter{versionnrten} \fi
      \ifnum\theversionnrten=10\stepcounter{versionnr} \fi
      \@skrivner
    \fi
    \theversionnr .\theversionnrten\theversionnrhundred
  }
}
%    \end{macrocode}
%  All other options which may be specified are to be 
%  ignored:
%    \begin{macrocode}
\DeclareOption*{\typeout{The option `\CurrentOption '\space 
  is not used by `version'.}}
%    \end{macrocode}
%  \par The counters used and the boolean |KeepVersion| 
%  are to be defined next.  Also the user commands 
%  |\keepversion| and |\setpversion|, which only (re-)set 
%  the |KeepVersion| boolean, are defined.
%    \begin{macrocode}
\newcounter{versionnr}
\newcounter{versionnrten}[versionnr]
\newcounter{versionnrhundred}[versionnrten]
\@addtoreset{versionnrhundred}{versionnr}
\newif\ifKeepVersion \KeepVersionfalse
\newcommand{\keepversion}{\global\KeepVersiontrue}
\newcommand{\stepversion}{\global\KeepVersionfalse}
%    \end{macrocode}
%  \par Now the task is to define the internals.  The macro 
%  |\vrsFile| simply holds the name of the |.vrs|-file to 
%  use.  The macro |\@skrivner| writes the version number 
%  counters to a file\footnote{This part is resposnible for 
%  the problems with the extra space when 
%  \texttt{$\backslash$keepversion} is not in effect as 
%  mentioned in the section ``Known Problems''.}
%  in a format which is easy to deal with in 
%  \LaTeX{}, the ordinary |\label| format.  The internal 
%  macro |\@vrs| is used to avoid problems with label 
%  change warnings and to avoid trouble with missing 
%  version number information in the |.aux|-file when 
%  |\keepversion| is used.  As an aid, the dummy-counter 
%  |VrsNr| is used.
%    \begin{macrocode}
\def\vrsFile{\jobname.vrs}
\newwrite\@vrs
\newcounter{VrsNr}
\def\@skrivner{%
  \setcounter{VrsNr}{\@partaux}  \addtocounter{VrsNr}{1}
  \let\@vrs=\theVrsNr
  \immediate\openout\@vrs\vrsFile
  \immediate\write\@vrs{\relax}
  \immediate\write\@vrs{%
    \string\newlabel{versionsnummer}{{\theversionnr}{\thepage}}}
  \immediate\write\@vrs{%
    \string\newlabel{versionsnummertio}{{\theversionnrten}{\thepage}}}
  \immediate\write\@vrs{%
    \string\newlabel{versionsnummerhundra}{{\theversionnrhundred}{\thepage}}}
  \immediate\closeout\@vrs{}
}
%    \end{macrocode}
%  \par Here comes the default version of the |\version| 
%  macro:
%    \begin{macrocode}
\newcommand{\version}{%
  \ifKeepVersion \else 
    \stepcounter{versionnr} 
    \@skrivner
  \fi
  \theversionnr
}
%    \end{macrocode}
%  \par To be able to smoothly read the |.vrs|-file and set 
%  the counters to zero the first time, a special 
%  `|\input|' macro is built.  This is then called on at 
%  the beginning of the document in the |\AtBeginDocument| 
%  command.
%    \begin{macrocode}
\def\@inp@t#1{\openin1 #1 \ifeof1 
    \typeout  {No file #1.}
    \setcounter{versionnr}{\z@}
    \setcounter{versionnrten}{\z@}
    \setcounter{versionnrhundred}{\z@}
  \else\closein1 \relax\@@input #1 
    \setcounter{versionnr}{\ref{versionsnummer}}
    \setcounter{versionnrten}{\ref{versionsnummertio}}
    \setcounter{versionnrhundred}{\ref{versionsnummerhundra}}
  \fi
}
\AtBeginDocument{%
  \@inp@t{\vrsFile}
}
%    \end{macrocode}
%  \par And, finally, lets adjust the |\version| command 
%  according to the specified option.
%    \begin{macrocode}
\ProcessOptions
%    \end{macrocode}
% \iffalse
%</paketkod>
%<*driver>
\documentclass[a4paper]{article}
\usepackage{doc}
\textwidth=150mm
\textheight=210mm
\topmargin=0mm
\oddsidemargin=5mm
\evensidemargin=5mm
\begin{document}
  \DocInput{version.dtx} 
  \PrintChanges
\end{document}
%</driver>
% \fi
%
% \Finale
%
\endinput
